package com.study.talk.module.controller;

import com.study.talk.common.exception.BadRequestException;
import com.study.talk.common.resullt.FwResult;
import com.study.talk.common.util.DateUtil;
import com.study.talk.common.util.MyFileUtils;
import com.study.talk.common.util.MyUtils;
import com.study.talk.common.util.SecurityUtil;
import com.study.talk.module.entity.User;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

@RestController
@RequestMapping("/file")
public class FileController {


    @Value("${file.imgPath}")
    String imgPath;

    @Value("${file.filePath}")
    String filePath;


    @ApiOperation("上传图片")
    @PostMapping("/upload/image")
    @ResponseBody
    public FwResult<String> uploadImg(@Valid @RequestBody MultipartFile mFile) throws IOException {
        User user = SecurityUtil.getUser();
        String path = saveImageFile(mFile, user);
        return FwResult.ok(path,"上传成功");
    }

    @ApiOperation("未登录上传图片")
    @PostMapping("/upload/image/other")
    @ResponseBody
    public FwResult<String> uploadImgNoLogin(@Valid @RequestBody MultipartFile mFile) throws IOException {

        if (mFile == null){
            return FwResult.failed("文件不存在");
        }
        String path = saveImageFile(mFile);
        return FwResult.ok(path,"上传成功");
    }

    @ApiOperation("上传多张图片")
    @PostMapping("/upload/image/multiple")
    @ResponseBody
    public FwResult<ArrayList<String>> uploadMultipleFile(MultipartFile[] files) throws IOException {
        ArrayList<String> results = new ArrayList<>();
        User user = SecurityUtil.getUser();
        for (MultipartFile multipartFile : files) {
            String path = saveImageFile(multipartFile,user);
            if (path.equals("")) return FwResult.failed(new ArrayList<>(),"文件不存在");
            results.add(path);
        }
        return FwResult.ok(results);
    }

    @ApiOperation("上传多张图片(未登录)")
    @PostMapping("/upload/image/multiple/other")
    @ResponseBody
    public FwResult<ArrayList<String>> uploadMultipleFileNoLogin(@RequestParam MultipartFile[] files) throws IOException {
        ArrayList<String> results = new ArrayList<>();
        for (MultipartFile multipartFile : files) {
            String path = saveImageFile(multipartFile);
            if (path.equals("")) return FwResult.failed(new ArrayList<>(),"文件不存在");
            results.add(path);
        }
        return FwResult.ok(results);
    }



    private void isExists(File file) {
        if (!file.exists()) {
            file.mkdirs();
        }
    }

    public String saveImageFile(MultipartFile MFile,User user) throws IOException {
        if (MFile == null){
            return "";
        }
        String currentTime = DateUtil.datePath();
        String path = imgPath+user.getId()+"/"+DateUtil.datePath();
        File file = new File(path);
        isExists(file);
        //获取文件名称
        String originalFilename = MFile.getOriginalFilename();
        if (originalFilename == null){
            throw new BadRequestException(404,"文件名称错误");
        }
        String filePath = file.getPath() + File.separator + MyUtils.UUID()+ MyFileUtils.getFileSuffix(originalFilename);
        File result = new File(filePath);
        MFile.transferTo(result);
        return "/"+user.getId()+"/"+currentTime+"/"+result.getName();
    }

    public String saveImageFile(MultipartFile MFile) throws IOException {
        if (MFile == null){
            return "";
        }
        String currentTime = DateUtil.datePath();
        String path = imgPath+"other"+"/"+currentTime;
        File file = new File(path);
        //判断文件夹是否存在
        isExists(file);
        String originalFilename = MFile.getOriginalFilename();
        //获取文件名称
        if (originalFilename == null){
            throw new BadRequestException(404,"文件名称错误");
        }
        String filePath = file.getPath() + File.separator + MyUtils.UUID()+ MyFileUtils.getFileSuffix(originalFilename);
        File result = new File(filePath);
        MFile.transferTo(result);
        return "/"+"other"+"/"+currentTime+"/"+result.getName();
    }

}
